package com.smit.test;

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;

import com.smit.adapter.DVBAdapter;
import com.smit.dvb.DVBTunerInfo;
import com.smit.dvb.scan.IScanCallback;
import com.smit.dvb.scan.ScanControl;
import com.smit.icast.ICastInitializer;
import com.smit.player.SmitPlayer;
import com.smit.player.SmitXPlayer;
import com.smit.utils.LogUtil;

/**
 * 测试用activity
 */
public class MainActivity extends Activity implements IScanCallback, Handler.Callback {
    private static final String TAG = "MainActivity";
    private static final String SMIT_X_PLAYER_CLASS_NAME = "com.smit.player.SmitXPlayer";
    private static final String APP_ID = "icast2016";
    private static final String APP_KEY = "icast_test_key";

    private ScanControl scanControl;
    private Handler handler;
    private MediaPlayer smitPlayer;
    private TextView msgTextView;
    private int delay = 1000;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        msgTextView = (TextView) findViewById(R.id.tv_main_msg);
        msgTextView.setText("*****iCast SDK Test*****\n");
        ICastInitializer.getInstance().initializeICast(this.getApplicationContext(), APP_ID, APP_KEY);
        scanControl = DVBAdapter.getInstance().getDVBManager().getScanControl();
        handler = new Handler(this);
        smitPlayer = SmitPlayer.getPlayer();
        msgTextView.append("onCreate\n");
        msgTextView.append("player:\n");
        try {
            Class<?> classType = Class.forName(SMIT_X_PLAYER_CLASS_NAME); //检查系统是否有SmitXPlayer
            if (classType != null && smitPlayer instanceof SmitXPlayer) {
                msgTextView.append(((SmitXPlayer) smitPlayer).getPlayerInfo());
            }
        } catch (ClassNotFoundException e) {
            msgTextView.append(((SmitPlayer) smitPlayer).getPlayerInfo());
            LogUtil.info(TAG, " SmitXPlayer NOT found, use SmitPlayer instead");
            //e.printStackTrace();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        msgTextView.append("onResume\n");
        DVBTunerInfo tunerInfo = new DVBTunerInfo();
        scanControl.addCallback(this);
//        scanControl.autoScan(135000, 150000);
//        scanControl.fullScan(0,1);
        scanControl.manualScan(tunerInfo);
    }

    @Override
    public void handleScanCallbackMsg(int msg, Object param) {
        Message handlerMsg = handler.obtainMessage(msg, param);
        handler.sendMessageDelayed(handlerMsg, delay);
        delay += 1000;
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case ScanControl.MSG_SCAN_START:
                //Toast.makeText(this, "scan start", Toast.LENGTH_SHORT).show();
                msgTextView.append("scan start>>>\n");
                break;
            case ScanControl.MSG_SCAN_TUNER_LOCKED:
                //Toast.makeText(this, "scan tuner locked", Toast.LENGTH_SHORT).show();
                msgTextView.append("scan tuner locked\n");
                break;
            case ScanControl.MSG_SCAN_PROGRESS_CHANGED:
                //Toast.makeText(this, "scan progress changed", Toast.LENGTH_SHORT).show();
                msgTextView.append("scan progress changed\n");
                break;
            case ScanControl.MSG_SCAN_SERVICES_FOUND:
                //Toast.makeText(this, "scan services found", Toast.LENGTH_SHORT).show();
                msgTextView.append("scan services found\n");
                break;
            case ScanControl.MSG_SCAN_FINISH:
                //Toast.makeText(this, "scan finish", Toast.LENGTH_SHORT).show();
                msgTextView.append("scan finish<<<\n");
                delay = 0;
                break;
            case ScanControl.MSG_SCAN_ERROR:
                //Toast.makeText(this, "scan error", Toast.LENGTH_SHORT).show();
                msgTextView.append("scan error!!!\n");
                break;
            default:
                msgTextView.append("what happened!!!\n");
                break;
        }
        return false;
    }

    @Override
    protected void onDestroy() {
        DVBAdapter.getInstance().release();
        super.onDestroy();
    }
}
